package com.flea.hclv.controller;

import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.dao.OrgCertificationMapper;
import com.flea.system.entity.OrgCertification;
import com.flea.system.entity.OrgUser;
import com.flea.system.security.ILoginInfo;
import com.flea.system.security.LoginCheck;
import com.flea.system.service.IOrgUserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/flea/hclv/forAddLogin")
public class LoginNoKeyController {
  @Autowired
  private IOrgUserService iOrgUserService;
  @Autowired
  private OrgCertificationMapper orgCertificationMapper;
    @ResponseBody
    @RequestMapping("/logins")
    public ServerResponse loginNoKey(HttpServletRequest request){
      HttpSession session = request.getSession();
      String model = request.getParameter("model");
      if(StringUtil.isEmpty(model)){
        return ServerResponse.createByErrorMessage("登录参数非法");
      }
      JSONObject jsb = JSONObject.parseObject(model);
      if(jsb==null){
        return ServerResponse.createByErrorMessage("参数转版失败");
      }

      String code = jsb.getString("code");
      String password=jsb.getString("password");
      if(StringUtil.isEmpty(code)){
        return ServerResponse.createByErrorMessage("用户名为空");
      }

      if(StringUtil.isEmpty(password)){
        return ServerResponse.createByErrorMessage("密码为空");
      }
      OrgUser user = iOrgUserService.getUserByCode(code);
      if(null==user){
        return ServerResponse.createByErrorMessage("该用户不存在");
      }

      String userId = user.getUserUuid();
      OrgCertification orgCertification =  orgCertificationMapper.findByUserUuid(userId);

      if(password.equals(orgCertification.getPassword())){
        LoginCheck chk = new LoginCheck();
        chk.setCode(user.getUserCode());
        chk.setName(user.getUserName());
        chk.setId(user.getUserUuid());

        session.setAttribute(ILoginInfo.ID_userInfoInSesion,chk);
        session.setAttribute("user_id",user.getUserUuid());
        return ServerResponse.createBySuccessMessage("登录成功");
      }else
      {
        return ServerResponse.createByErrorMessage("登录失败，密码错误");
      }
    }
}
